Public Class ErrorHandler
    Private MsgBoxIsShown As Boolean = True

    Public Sub ErrorMsgBox(ByVal Ex As Exception, Optional ByVal Message As String = "")
        If MsgBoxIsShown Then
            MsgBox(Ex.ToString & vbCrLf & Message, MsgBoxStyle.OkOnly, Ex.Source)
        Else
            ErrorLog(Ex.Source.ToString, "", "Error", Mid(Ex.ToString, 1, InStr(Ex.ToString, vbCrLf) - 1))
        End If
    End Sub

    Public Sub ErrorLog(ByVal ClassName As String, ByVal RoutineName As String, ByVal Name As String, ByVal Value As String)
        Dim LogTxt As String = ""

        LogTxt &= Now.ToString & vbTab

        LogTxt &= ClassName
        If RoutineName = "" Then
            LogTxt &= vbTab
        Else
            LogTxt &= "." & RoutineName & vbTab
        End If

        LogTxt &= Name & vbTab
        LogTxt &= Value

        Try
            FileOpen(1, LogFilePath, OpenMode.Append, OpenAccess.Write, OpenShare.Shared)
            Print(1, LogTxt & vbCrLf)
            FileClose(1)
        Catch ex As Exception
            FileClose(1)
        End Try
    End Sub

    Public Function LogFilePath() As String
        Dim path As String = My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData.ToString
        Return My.Computer.FileSystem.CombinePath(path, "ErrorLog.txt")
    End Function
End Class
